#!/bin/bash

let firstAttribute=0

function addAttribute {
    if [[ -z "$1" ]] || [[ -z "$2" ]] ; then
        return;
    fi

    if [[ $firstAttribute -eq 0 ]] ; then
        let firstAttribute=1
    else
        echo "," >> $outputDir"/"$multiverseid.json
    fi
    echo -n "    \"$1\" : $2" >> $outputDir"/"$multiverseid.json;
}

outputDir="."

for var in "$@"
do
    if [[ "$var" =~ ^"--"(multiverseid|m)"="([0-9]+)$ ]] ; then
        multiverseid=${BASH_REMATCH[2]}
    elif [[ "$var" =~ ^"--outputdir="(.+)$ ]] || [[ "$var" =~ ^"--o="(.+)$ ]] ; then
        outputDir=${BASH_REMATCH[1]}
    else
        echo "unknown argument $var"
        echo "Usage: bash getData.sh --multiverseid=<number>"
        exit 1
    fi
done

if [[ -z $multiverseid ]] ; then
    echo "Error: multiverseid not set"
    exit 1
fi

wget "http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=$multiverseid" -O currentFile.aspx 1> /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
    echo "could not download file"
    exit 1
fi

echo "{" > $outputDir"/"$multiverseid.json
addAttribute "image" "\"http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=$multiverseid&type=card\""

while read line ; do
    if [[ "$line" =~ ^"Card Name:</div>" ]] ; then
        read; read line2;
        name=$(echo "$line2" | grep -Po '.+(?=</div>)')
        
        addAttribute "name" "\"$name\""
    elif [[ "$line" =~ ^"Mana Cost:</div>" ]] ; then
        read; read line2;
        manacostSymbols=$(echo "$line2" | grep -Po '(?<=alt=")[^"]+(?=")')

        for manacostSymbol in $manacostSymbols; do
            if [[ $manacost == *Variable || $manacost == *or || $manacostSymbol == or ]] ; then
                manacost=$manacost" "$manacostSymbol
            else 
                manacost=$manacost"|"$manacostSymbol
            fi
        done
        manacost=${manacost:1}
        
        unset IFS
        
        addAttribute "manacost" "\"$manacost\""
    elif [[ "$line" =~ ^"Converted Mana Cost:</div>" ]] ; then
        read; read line2;
        convertedManacost=$(echo "$line2" | grep -Po '[0-9]+(?=\<)')

        addAttribute "converted manacost" $convertedManacost
    elif [[ "$line" =~ ^"Types:</div>" ]] ; then
        read; read line2;
        
        types=$(echo "$line2" | grep -Po '^.+(?=</div>)')
        
        mainTypes=$(echo "$types" | grep -Po '^[^—]+')
        subTypes=$(echo "$types" | grep -Po '(?<=—).+')
        
        addAttribute "types" "\"${mainTypes%"${mainTypes##*[![:space:]]}"}\""
        if [[ -n $subTypes ]]; then
            addAttribute "subtypes" "\"${subTypes#"${subTypes%%[![:space:]]*}"}\""
        fi
    elif [[ "$line" =~ ^"Card Text:</div>" ]] ; then
        read; read line2;
        
        cardTexts=$(echo "$line2" | grep -Po '(?<=(<div class="cardtextbox" style="padding-left:10px;">)).+?(?=(</div>))')
        
        IFS=$'\n'
        
        cardTextJson=""
        for cardText in $cardTexts ; do
            cardText=`echo $cardText | sed -e 's/<img src="[^"]*" alt="/{/g' -e 's/" align="absbottom" \/>/}/g' -e 's/"/\\\"/g'`
            cardTextJson=$cardTextJson", \"$cardText\""
        done
        cardTextJson="["${cardTextJson:2}"]"
        
        addAttribute "card text" $cardTextJson
        
        unset IFS
    elif [[ "$line" =~ ^"Flavor Text:</div>" ]] ; then
        read; read line2;
    
        flavortext=$(echo "$line2" | grep -Po '(?<=<i>).+(?=</i>)' | sed -e 's/\r//g')
        flavortext=${flavortext//"</i>"/}
        flavortext=${flavortext//"</div>"/}
        flavortext=${flavortext//"<div class='cardtextbox'>"/}
        flavortext=${flavortext//\"/\\\"}
        flavortext=${flavortext//"$(printf '\\x%x' 0x0d)"/}
        
        addAttribute "flavor text" "\"$flavortext\""
    
    elif [[ "$line" =~ ^"Watermark:</div>" ]] ; then
        read; read line2;
    
        watermark=$(echo "$line2" | grep -Po '(?<=<i>).+(?=</i>)')
        
        addAttribute "watermark" "\"$watermark\""
        
    elif [[ "$line" =~ ^"<b>P/T:</b></div>" ]] ; then
        read; read line2;
        
        power=$(echo "$line2" | grep -Po '.+(?=\ /)')
        addAttribute "power" "\"$power\""
        
        toughness=$(echo "$line2" | grep -Po '(?<=/\ )[^<]+')
        
        addAttribute "toughness" "\"$toughness\""
        
    elif [[ "$line" =~ ^"Loyalty:</div>" ]] ; then
        read; read line2;
        
        loyalty=$(echo "$line2" | grep -Po '[0-9]+(?=</div>)')
        addAttribute "loyalty" "$loyalty"
        
    elif [[ "$line" =~ ^"Expansion:</div>" ]] ; then
        read; read; read; read line2;
        
        expansion=$(echo "$line2" | grep -Po '(?<=>).+(?=</a>)')

        addAttribute "expansion" "\"$expansion\"";

    elif [[ "$line" =~ ^"Rarity:</div>" ]] ; then
        read; read line2;
        
        rarity=$(echo "$line2" | grep -Po '(?<=>).+(?=</span>)')
        
        addAttribute "rarity" "\"$rarity\"";
    elif [[ "$line" =~ ^"Card Number:</div>" ]] ; then
        read; read line2;
        
        cardnumber=$(echo "$line2" | grep -Po '[0-9]+(?=</div>)')
        
        addAttribute "card number" "$cardnumber";
    elif [[ "$line" =~ ^"Artist:</div>" ]] ; then
        read; read line2;
        
        artist=$(echo "$line2" | grep -Po '(?<=>).+(?=</a>)')
        
        addAttribute "artist" "\"$artist\"";

    fi
done < "currentFile.aspx"

echo "" >> $outputDir"/"$multiverseid.json
echo "}" >> $outputDir"/"$multiverseid.json

rm currentFile.aspx
